﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using fuzhu;
using LH.Model.PU;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Ocsp;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WMS;
using Yonyou.U8.BO;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace XXX.api.Controllers.XianFeng
{


    /// <summary>
    /// 生成货位调整单
    /// </summary>
    [Route("api/wms/[controller]")]
    [ApiController]
    //[Authorize]
    public class ST_AdjustPVouchController : ControllerBase
    {
        private readonly ILogger<ST_AdjustPVouchController> _logger;
        private readonly IEmailService _EmailService;

        public ST_AdjustPVouchController(ILogger<ST_AdjustPVouchController> logger, IEmailService emailService)
        {
            //_invRepository = xxxRepository;
            _logger = logger;
            _EmailService = emailService;
        }

        [HttpPost]
        public async Task<RetModel2> Add(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            string cType = "货位调整";
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            #region 接口调用间隔
            string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

            if (CacheHelpCore.GetCacheValue(strKey) == null)
            {
                CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
            }
            else
            {
                throw new Exception("接口调用间隔过短");
            }
            #endregion 接口调用间隔
            try
            {
                string cErr = "";
                ScanBarcodeVM model = JsonConvert.DeserializeObject<ScanBarcodeVM>(requestBody);

                RetModel2 ret = new RetModel2();
                if (model == null)
                {
                    ret = DBService.ReErrObject("没有数据");
                }
                else
                {

                    ret = model.ST_AdjustPVouch();
                }
                // 将 JSON 数据保存到数据库中
                JsonSaveService.saveLog2(cType, model.cCode, requestBody, ret.code, ret.message);
                new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, ret.code, CanShu.mail.ToAddress, CanShu.cZT + cType + model.cCode, "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ret.message);



                return ret;
            }
            catch (Exception ex)
            {
                new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, 500, CanShu.mail.ToAddress, CanShu.cZT + cType + "程序错误", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);

                return DBService.ReErrObject(ex.Message);
            }
        }




    }
















}
